<?php

namespace app\api\middleware;

use app\common\repositories\apps\AppsRepository;
use app\common\repositories\users\UsersRepository;
use support\Container;
use Webman\MiddlewareInterface;
use Webman\Http\Request;
use Webman\Http\Response;
use support\Db;

class DeviceMiddleware implements MiddlewareInterface
{
    public function process(Request $request, callable $next): Response
    {
        
        $appkey = get_appkey();
        $version = get_version();
        $fingerprint = get_fingerprint();

        if(empty($appkey)) {
            return json(['code' => 410, 'msg' => ' No appkey'], 410);
        }
        if(empty($version)) {
            return json(['code' => 410, 'msg' => ' No version'], 410);
        }
        if(empty($fingerprint)) {
            return json(['code' => 410, 'msg' => ' No fingerprint'], 410);
        }

        $appinfo = Container::make(AppsRepository::class)->getOneAppInfoByAppkey($appkey);

        if (empty($appinfo)) {
            return json(['code' => 410, 'msg' => 'No appkey'], 410);
        }
        $request->appinfo = $appinfo;
        $request->appkey = $appkey;
        $request->version = $version;
        $request->fingerprint = $fingerprint;
        
        return $next($request);
    }
}
